using UnityEngine;

namespace Brick
{
	public class Bricks : MonoBehaviour
	{
		private int timesHits;

		private LevelManager levelManger;

		private bool isBreakable;

		public Sprite[] hitSprites;

		public static int breakableCount;

		public AudioClip crack;

		public GameObject splash;

		private void Start()
		{
			timesHits = 0;
			levelManger = UnityEngine.Object.FindObjectOfType<LevelManager>();
			isBreakable = (base.gameObject.tag == "Breakable");
			if (isBreakable)
			{
				breakableCount++;
			}
		}

		private void Update()
		{
		}

		private void OnCollisionEnter2D(Collision2D col)
		{
			if (isBreakable)
			{
				HandleHits();
			}
			AudioSource.PlayClipAtPoint(crack, base.transform.position);
		}

		private void HandleHits()
		{
			timesHits++;
			int num = hitSprites.Length + 1;
			if (timesHits >= num)
			{
				UnityEngine.Object.Destroy(base.gameObject);
				PuffSmoke();
				breakableCount--;
				levelManger.BrickDestroyed();
			}
			else
			{
				LoadSprites();
			}
		}

		private void PuffSmoke()
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(splash, base.transform.position, Quaternion.identity);
			//gameObject.GetComponent<ParticleSystem>().main.startColor = base.gameObject.GetComponent<SpriteRenderer>().color;
		}

		private void LoadSprites()
		{
			int num = timesHits - 1;
			if ((bool)hitSprites[num])
			{
				GetComponent<SpriteRenderer>().sprite = hitSprites[num];
			}
		}
	}
}

